//
//  EWShoppingHeaderView.m
//  haveSomethingToEat
//
//  Created by 陈 宁权 on 2017/7/4.
//  Copyright © 2017年 陈 宁权. All rights reserved.
//

#import "EWShoppingHeaderView.h"
#import "DefaultPortraitView.h"

@interface EWShoppingHeaderView()
@property (nonatomic,strong) UIImageView *logoView;
@property (nonatomic,strong) UILabel *businessName;
@property (nonatomic,strong) UILabel *address;
@property (nonatomic,strong) XHStarRateView *starRateView;
@end

@implementation EWShoppingHeaderView

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"EWShoppingHeaderView";
    EWShoppingHeaderView *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[EWShoppingHeaderView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupChildViews];
        self.backgroundColor = [UIColor colorWithRed:133/255.0 green:32/255.0 blue:133/255.0 alpha:1];
    }
    return self;
}

- (void)setupChildViews
{
    _logoView = [[UIImageView alloc] init];
    _logoView.layer.cornerRadius = 5.0;
    _logoView.layer.masksToBounds = YES;
    [self addSubview:_logoView];
    
    _businessName = [[UILabel alloc] init];
    _businessName.textColor = [UIColor whiteColor];
    [self addSubview:_businessName];
    
    _address = [[UILabel alloc] init];
    _address.textColor = [UIColor whiteColor];
    _address.font = [UIFont systemFontOfSize:12];
    _address.textAlignment = NSTextAlignmentRight;
    [self addSubview:_address];
    
    _starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(0, 0, 40, 15)];
    _starRateView.isAnimation = YES;
    _starRateView.rateStyle = IncompleteStar;
    _starRateView.tag = 1;
    [self addSubview:_starRateView];
    
    [_logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(5);
        make.bottom.equalTo(self).offset(-5);
        make.left.equalTo(self).offset(5);
        make.width.offset(80*KWidth_Scale);
    }];
    
    [_businessName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_logoView.mas_top).offset(5);
        make.left.equalTo(_logoView.mas_right).offset(5);
        make.right.equalTo(self).offset(-5);
        make.height.offset(20);
    }];
    
    [_starRateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_logoView.mas_bottom).offset(-5);
        make.left.equalTo(_logoView.mas_right).offset(5);
        make.width.offset(40);
        make.height.offset(15);
    }];
    
    [_address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_starRateView);
        make.left.equalTo(_starRateView.mas_right).offset(5);
        make.right.equalTo(self).offset(-5);
        make.height.offset(15);
    }];
}

- (void)setBusinessInfo:(EWBusinessInfo *)businessInfo
{
    _businessInfo = businessInfo;
    if (_businessInfo.portraitUri.length < 1) {
        DefaultPortraitView *defaultPortrait =
        [[DefaultPortraitView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [defaultPortrait setColorAndLabel:self.businessInfo.businessId
                                 Nickname:self.businessInfo.businessName];
        UIImage *portrait = [defaultPortrait imageFromView];
        _logoView.image = portrait;
    }
    else
    {
        [_logoView sd_setImageWithURL:[NSURL URLWithString:_businessInfo.portraitUri]];
    }
    
    _businessName.text = _businessInfo.businessName;
    _starRateView.currentScore = [_businessInfo.score floatValue];
    _address.text = _businessInfo.fullAdress;
}

@end
